<?php

namespace App\Http\Controllers\Backend;

use App\Http\Requests\Config\Yun\CreateRequest;
use App\Http\Requests\Config\Yun\UpdateRequest;
use App\Models\Qiniu;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Http\Response;

class QiNiuConfigController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $user = \Auth::user();
        if (!$user->hasPermissionTo('云存储')) {
            return view('error.error');
        }
        $configs = Qiniu::paginate(15);
        return view('qiniu.list', ['configs' => $configs]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        $user = \Auth::user();
        if (!$user->hasPermissionTo('云存储')) {
            return view('error.error');
        }
        return view('qiniu.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param CreateRequest $request
     * @return Response
     * @throws \Throwable
     */
    public function store(CreateRequest $request)
    {
        $attributes = $request->validated();
        $model = \DB::transaction(function () use ($attributes) {
            $model = Qiniu::create($attributes);
            return $model;
        });
        return redirect()->route('qi-niu-config.index')->with('success', '添加成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param Qiniu $qi_niu_config
     * @return Response
     */
    public function edit(Qiniu $qi_niu_config)
    {
        $user = \Auth::user();
        if (!$user->hasPermissionTo('云存储')) {
            return view('error.error');
        }
        return view('qiniu.create', ['config' => $qi_niu_config]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param UpdateRequest $request
     * @param Qiniu $qi_niu_config
     * @return void
     * @throws \Throwable
     */
    public function update(UpdateRequest $request, Qiniu $qi_niu_config)
    {
        $attributes = $request->validated();
        $model = \DB::transaction(function () use ($attributes, $qi_niu_config) {
            $model = $qi_niu_config->update($attributes);
            return $model;
        });
        return redirect()->route('qi-niu-config.index')->with('success', '添加成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }
}
